﻿package com.eve.eplus.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.eve.eplus.R;
import com.eve.eplus.common.activity.BaseActivity;
import com.eve.eplus.common.adapter.AccountManagerAdapter;
import com.eve.eplus.common.app.BaseApplication;
import com.eve.eplus.common.app.Constants;
import com.eve.eplus.common.view.ComToastTools;
import com.eve.eplus.model.AccountModel;
import com.eve.eplus.sns.SNSAccountManager;
import com.eve.eplus.sns.SNSContentManager;

/**
 * 帐号管理界面
 * 
 * @author Norsie
 * @create 12-11-29
 */
public class AccountManagerActivity extends BaseActivity
{

    private Context mContext = AccountManagerActivity.this;
    private LinearLayout btnAdd, btnDelete;
    private ListView mListview;
    private ArrayList<HashMap<String, String>> mSelectList = new ArrayList<HashMap<String, String>>();
    private AccountManagerAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_main);
        initView();
        mSelectList = SNSAccountManager.getAccountList();
        adapter = new AccountManagerAdapter(mContext,
                                            BaseApplication.SNS_ACCOUNT);
        mListview.setAdapter(adapter);

    }

    /**
     * 界面初始化
     */
    private void initView()
    {
        mListview = (ListView) this.findViewById(R.id.id_account_listview);
        btnAdd = (LinearLayout) this.findViewById(R.id.id_account_add);
        btnDelete = (LinearLayout) this.findViewById(R.id.id_account_delete);

        mListview.setOnItemClickListener(onItemClickListener);
        btnAdd.setOnClickListener(onClickListener);
        btnDelete.setOnClickListener(onClickListener);

        btnDelete.setVisibility(View.GONE);

    }

    /**
     * Listview项 点击事件
     */
    OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id)
        {
            RelativeLayout mBackground = (RelativeLayout) view.findViewById(R.id.id_item_account);
            if (!mSelectList.get(position).containsKey("isSelect"))
            {
                mSelectList.get(position).put("isSelect", "true");
                mBackground.setBackgroundColor(Color.parseColor("#37aff4"));
            }
            else
            {
                if (mSelectList.get(position).get("isSelect").equals("true"))
                {
                    mSelectList.get(position).put("isSelect", "false");
                    mBackground.setBackgroundColor(Color.TRANSPARENT);
                }
                else
                {
                    mSelectList.get(position).put("isSelect", "true");
                    mBackground.setBackgroundColor(Color.parseColor("#37aff4"));
                }
            }

            for (int i = 0; i < mSelectList.size(); i++)
            {
                if (mSelectList.get(i).containsKey("isSelect"))
                    if (mSelectList.get(i).get("isSelect").equals("true"))
                    {
                        btnDelete.setVisibility(View.VISIBLE);
                        break;
                    }
                if (i == mSelectList.size() - 1)
                    btnDelete.setVisibility(View.GONE);
            }
        }
    };

    /**
     * 点击事件
     */
    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.id_account_add:
                    Intent intent=new Intent(mContext, BindingActivity.class);
                    intent.putExtra(Constants.ACTIVITY_SOURCE, Constants.ACTIVITY_SOURCE_ACCOUNTACTIVITY);
                    gotoActivity(mContext, intent);
                    break;

                case R.id.id_account_delete:
                    deleteAccount();
                    break;
                default:
                    break;
            }

        }

    };

    /**
     * 删除帐号
     */
    private void deleteAccount()
    {
        List<AccountModel> list = new ArrayList<AccountModel>();
        for (int i = 0; i < mSelectList.size(); i++)
        {
            if (mSelectList.get(i).containsKey("isSelect"))
                if (mSelectList.get(i).get("isSelect").equals("true"))
                {
                    AccountModel model = new AccountModel();
                    model.setId(mSelectList.get(i).get(Constants.ACCOUNT_ID));
                    list.add(model);
                    SNSContentManager.delete(mSelectList.get(i).get(
                            Constants.SNS_UID));
                }
        }
        SNSAccountManager.delete(list);

        BaseApplication.SNS_ACCOUNT = SNSAccountManager.getAccountList();
        viewReflash();

    }

    /*
     * 刷新界面和数据
     */
    private void viewReflash()
    {
        mSelectList = BaseApplication.SNS_ACCOUNT;
        adapter.reflash(mSelectList);
        btnDelete.setVisibility(View.GONE);
        // 将之前的选中背景颜色恢复
        for (int i = 0; i < mSelectList.size(); i++)
        {
            if (mListview.getChildAt(i) != null)
                mListview.getChildAt(i).findViewById(R.id.id_item_account).setBackgroundColor(
                        Color.TRANSPARENT);
        }

    }

    @Override
    protected void onRestart()
    {
        // TODO Auto-generated method stub
        super.onRestart();
        viewReflash();
    }

    @Override
    public void onBackPressed()
    {
        if (BaseApplication.SNS_ACCOUNT.size() == 0)
        {
            ComToastTools.ShowMsg(mContext, "请先绑定帐号", "s");
            Intent intent = new Intent(mContext, BindingActivity.class);
            intent.putExtra(Constants.ACTIVITY_TARGET,
                    Constants.ACTIVITY_TARGET_HOMEACTIVITY);
            startActivity(intent);
            finish();
        }
        else
        {
            super.onBackPressed();
            onDestroy();
        }
    }

    //
    // // private DraggableListView.DropListener onDrop = new
    // DraggableListView.DropListener() {
    // // @Override
    // // public void drop(int from, int to) {
    // // String item = adapter.getItem(from);
    // //
    // // adapter.remove(item);
    // // adapter.insert(item, to);
    // // Toast.makeText(mContext, adapter.getList().toString(),
    // // Toast.LENGTH_SHORT).show();
    // // mChannelChanged=adapter.getList(); //保存变化后的排序
    // // ChannelListManage();
    // // }
    // // };
    // //
    // // private DraggableListView.RemoveListener onRemove = new
    // DraggableListView.RemoveListener() {
    // // @Override
    // // public void remove(int which) {
    // // adapter.remove(adapter.getItem(which));
    // // }
    // // };

}
